package ils.domain.issue.model;

import ils.util.HibernateSession;
import ils.util.HibernateSessionManager;
import ils.util.SpringUtil;
import junit.framework.Assert;
import org.junit.Test;
import org.springframework.context.ApplicationContext;

/**
 * Created with IntelliJ IDEA.
 * User: rejeevd
 * Date: 23/09/12
 * Time: 12:19
 * To change this template use File | Settings | File Templates.
 */
public class ItemRepositoryTest
{
    HibernateSessionManager hibernateSessionManager = new HibernateSessionManager();
    ApplicationContext context = SpringUtil.getApplicationContext();

    @Test
    public void testGetItem()
    {
        try(HibernateSession session = hibernateSessionManager.getSession())
        {
            ItemRepository repository = (ItemRepository)context.getBean("itemRepository");
            Item item = repository.getItem(1);
            Assert.assertNotNull(item);
            Assert.assertNotNull(item.getShelf());
        }
    }

    @Test
    public void testGetItemByBarcode()
    {
        try(HibernateSession session = hibernateSessionManager.getSession())
        {
            ItemRepository repository = (ItemRepository)context.getBean("itemRepository");
            Item item = repository.getItem("11111");
            Assert.assertNotNull(item);
            Assert.assertNotNull(item.getShelf());
        }
    }

    @Test
    public void testCreateItem()
    {
        try(HibernateSession session = hibernateSessionManager.getSession())
        {
            ItemRepository repository = (ItemRepository)context.getBean("itemRepository");
            ShelfRepository shelfRepository = (ShelfRepository)context.getBean("shelfRepository");
            Item item = new Item();
            item.setStatus("active");
            item.setBarcode("11112");
            item.setBookId(1);
            Shelf shelf = shelfRepository.getShelf(3);
            item.setShelf(shelf);
            item = repository.createItem(item);
            Assert.assertNotNull(item.getId());
        }
    }
}
